package com.ruoyi.business.sundry.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbCheats;
import com.ruoyi.business.sundry.domain.JbCheatsCategory;
import com.ruoyi.business.sundry.domain.dto.JbCheatsDto;
import com.ruoyi.business.sundry.mapper.JbCheatsMapper;
import com.ruoyi.business.sundry.service.JbCheatsCategoryService;
import com.ruoyi.business.sundry.service.JbCheatsService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 秘籍Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class JbCheatsServiceImpl
        extends JoBaseServiceImpl<JbCheatsMapper, JbCheats>
        implements JbCheatsService
{
    @Autowired
    private JbCheatsCategoryService jbCheatsCategoryService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    @Transactional
    public Boolean saveInfo(JbCheats jbCheats)
    {
        boolean save = save(jbCheats);
        JbCheatsCategory byId = jbCheatsCategoryService.getById(jbCheats.getCategoryId());
        byId.setCheatsCount(byId.getCheatsCount() + 1);
        jbCheatsCategoryService.updateById(byId);
        return save;
    }

    @Override
    public IPage<JbCheatsDto> managerList(PageParam page, JbCheatsDto jbCheats)
    {
        LambdaQueryWrapper<JbCheats> select = new LambdaQueryWrapper<>();
        select.eq(StringUtils.isNotEmpty(jbCheats.getType()), JbCheats::getType, jbCheats.getType())
                .eq(ObjectUtils.isNotEmpty(jbCheats.getCategoryId()), JbCheats::getCategoryId, jbCheats.getCategoryId())
                .like(StringUtils.isNotBlank(jbCheats.getTitle()), JbCheats::getTitle, jbCheats.getTitle())
                .eq(ObjectUtils.isNotEmpty(jbCheats.getStatus()), JbCheats::getStatus, jbCheats.getStatus())
                .ge(StringUtils.isNotBlank(page.getStartTime()), JbCheats::getCreateTime, page.getStartTime())
                .le(StringUtils.isNotBlank(page.getEndTime()), JbCheats::getCreateTime, page.getEndTime())
                .and(ObjectUtils.isNotEmpty(jbCheats.getCategoryIds()), a ->
                {
                    a.eq(JbCheats::getCategoryId, jbCheats.getCategoryIds().get(0));
                    for (Long cid : jbCheats.getCategoryIds())
                    {
                        a.or().eq(JbCheats::getCategoryId, cid);
                    }
                });
        select.orderByDesc(JbCheats::getCreateTime);
        IPage<JbCheats> iPage = this.page(page.b(), select);
        IPage<JbCheatsDto> pageVo = new Page<>();
        BeanUtils.copyProperties(iPage, pageVo);
        List<JbCheatsDto> listVo = Lists.newArrayList();
        if (ObjectUtils.isNotEmpty(iPage.getRecords()))
        {
            List<Long> userIds = iPage.getRecords().stream().map(JbCheats::getCreateBy).collect(Collectors.toList());
            List<SysUser> list = sysUserService.list(Wrappers.lambdaQuery(SysUser.class).in(SysUser::getUserId, userIds)
                    .select(SysUser::getUserId, SysUser::getUserName));
            Map<Long, String> mapUser = list.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
            iPage.getRecords().forEach(a ->
            {
                JbCheatsDto jbCheatsDto = BeanUtil.toBean(a, JbCheatsDto.class);
                jbCheatsDto.setUserName(mapUser.get(a.getCreateBy()));
                jbCheatsDto.setTime(DateUtils.getTimeFormatText(a.getCreateTime()));
                listVo.add(jbCheatsDto);
            });
        }
        pageVo.setRecords(listVo);
        return pageVo;
    }

    @Override
    @Transactional
    public Boolean deleteInfo(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        // 修改宝典分类的数据减1
        list.forEach(a ->
        {
            JbCheats byId = this.getById(a);
            JbCheatsCategory byId1 = jbCheatsCategoryService.getById(byId.getCategoryId());
            if (ObjectUtils.isNotEmpty(byId1)
                    && ObjectUtils.isNotEmpty(byId1.getCheatsCount())
                    && byId1.getCheatsCount() > 0)
            {
                byId1.setCheatsCount(byId1.getCheatsCount() - 1);
                jbCheatsCategoryService.updateById(byId1);
            }
        });
        this.removeByIds(list);
        return true;
    }
}
